import { defineStore } from 'pinia'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import $ajaxGrid from '@/api/to8082.js'
import $ajax from '@/api/to8083.js'
import $ajaxAQI from '@/api/to8084.js'
import router from '@/router/index'
import { alertCode } from '@/utils/msg.js'

export const useFeedbacks = defineStore('feedbacks', {
  state: () => ({
    /** @type {{ id:number, rname:String, nbl:number, remark:String }[]} */
    allFeedbacks: [],
    feedbacks: [],
    members: [],
    curPage: 1,
    pageSize: 5,
    dialogVisible: false,
    aqis: [],
    curAqi: '',
    curFeedback: '',
    curInfo: '',
    states: ['待处理', '进行中', '已完成'],
    PCRs: [],
    index: 0,
    ids: [],
    gridors: [],
    gridor: '',
    value: '待处理',
    fullLoading: false
  }),
  getters: {
    filterData(state) {
      console.log(this.allFeedbacks)
      if (this.states.findIndex((item) => item === this.value) === 0) {
        return state.allFeedbacks.filter((item) => item.state === 0)
      } else if (this.states.findIndex((item) => item === this.value) === 1) {
        return state.allFeedbacks.filter((item) => item.state === 1)
      }
      return state.allFeedbacks.filter((item) => item.state === 2)
    }
  },
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    async loadData() {
      if (this.feedbacks === null || this.feedbacks.length === 0) {
        const path = '/feedback/a/list'
        const res = await $ajax.get(path)
        console.log(res.data)
        if (res.code === 200) {
          this.feedbacks = res.data
        }
      }
    },
    async loadDATA() {
      if (this.allFeedbacks === null || this.allFeedbacks.length === 0) {
        const path = '/feedback/a/undo'
        const res = await $ajax.get(path)
        console.log(res.data)
        if (res.code === 200) {
          this.allFeedbacks = res.data
        }
      }
    },
    async loadPCR() {
      const path = '/grid/list'

      const res = await $ajaxGrid.get(path)

      if (res.code === 200) this.PCRs = res.data
      console.log('PCR loaded', this.PCRs)
    },
    async loadAQIs() {
      const path = '/aqi/list'
      const res = await $ajaxAQI.get(path)
      if (res.code === 200) {
        this.aqis = res.data
      }
      console.log('aqis loaded')
      console.log(this.aqis)
    },
    async loadGridors(id) {
      const path = '/grid/gridors/' + id
      const res = await $ajaxGrid.get(path)
      if (res.code === 200) this.gridors = res.data
      console.log(this.gridors)
    },
    async distribute() {
      this.fullLoading = true
      setTimeout(() => {
        this.fullLoading = false
      }, 2000)
      const path = '/feedback/a/distribute'
      const res = await $ajax.post(path, { id: this.curFeedback.id, gid: this.gridor })
      if (res.code === 200) {
        this.allFeedbacks.find((i) => i.id === res.data.id).gid = this.gridor
        this.allFeedbacks.find((i) => i.id === res.data.id).gname = res.data.gname
        this.allFeedbacks.find((i) => i.id === res.data.id).state = res.data.state
        this.curFeedback = this.allFeedbacks.find((i) => i.id === res.data.id)
      }
    },
    load() {},
    detail(row) {
      this.curFeedback = row
      this.curInfo = row.feedbackInfo
      this.curAqi = this.aqis.find((item) => item.id === row.feedbackInfo.aqilevel)
      // 跳转到详情页
      router.push({ name: 'detail' })
    },
    undoDetail(index, row) {
      this.index = index
      console.log(row)
      this.curFeedback = row
      this.curInfo = row.feedbackInfo

      // 跳转到详情页
      router.push({ name: 'undo', query: { id: row.id } })
    },
    undoGoBack() {
      this.ids = []
      this.gridors = []
      this.curFeedback = ''

      router.push({ name: 'public' })
    },
    goBack() {
      router.push({ name: 'feedbacks' })
    },
    undoCheckData() {
      this.index = useRoute().query.id
      if (this.curFeedback === '' || this.curInfo === '') router.push({ name: 'public' })
    },
    checkData() {
      if (this.curFeedback === '' || this.curInfo === '' || this.curAqi === '')
        router.push({ name: 'feedbacks' })
    },
    find() {
      console.log(useRoute().params)
    },
    clearGridors() {
      console.log('clear gridors')
      this.gridors = []
      this.gridor = ''
    },
    clear() {
      this.ids = []
      this.gridors = []
      this.gridor = ''
    },
    logAQI() {
      console.log('logI')
      console.log(this.index)
    }
  }
})
